package com.adv.ems.model.vo;

import cn.hutool.core.date.DateUtil;
import com.adv.ems.constants.EmsConst;
import com.adv.ems.db.entity.*;
import com.adv.ems.enums.CollectPointDataTypeEnum;
import com.adv.ems.utils.EmsUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Data
public class BasePointVO {
    private List<Info> list;
    private Integer total;

    public BasePointVO() {
        this.list = new ArrayList<Info>();
    }

    @EqualsAndHashCode(callSuper = true)
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Info extends CollectPointDO {
        private String functionName;
        private String gatewayName;
        private String meterName;
        private String dataTypeDesc;
        private String lastUploadTimeStr;
        private String lastUploadRealValueStr;
        private String virtualDesc;
        private String typeDesc;
        private String stateDesc;
        private Boolean health;
        private String healthDesc;

        public Info(CollectPointDO c, CollectPointFunctionDO functionDO, GatewayDeviceDO g, MeterDO m, String acceptName) {
            BeanUtils.copyProperties(c, this);
            if (functionDO != null) {
                this.functionName = EmsUtil.multilingual(functionDO.getNameInfo(), acceptName);
            }
            if (g != null) {
                this.setGatewayName(g.getName());
            }
            if (m != null) {
                this.setMeterName(m.getName());
            }
            if (this.getDataType() != null) {
                this.setDataTypeDesc(CollectPointDataTypeEnum.CUMULATE.name().equals(this.getDataType()) ? "累积": "过程");
            }
            if (this.getLastUploadTime() != null) {
                this.setLastUploadTimeStr(DateUtil.formatDateTime(this.getLastUploadTime()));
            }
            if (this.getLastUploadRealValue() != null) {
                this.setLastUploadRealValueStr(EmsUtil.valueFormat(this.getLastUploadRealValue()));
            }
            if (this.getVirtual() != null) {
                this.setVirtualDesc(this.getVirtual().equals(EmsConst.YES) ? "虚拟点": "真实点");
            }
            if (this.getType() != null) {
                this.setTypeDesc(this.getType().equals("USER") ? "用户创建": "系统创建");
            }
            Map.Entry<Boolean, String> deviceState = EmsUtil.getDeviceState(this.getLastUploadTime());
            this.setHealth(deviceState.getKey());
            this.setHealthDesc(deviceState.getValue());
            if (this.getState() != null) {
                this.setStateDesc("CONFIRM".equals(this.getState()) ? "待确认": ("REFUSE".equals(this.getState()) ? "拒绝处理数据": "正常"));
            }
        }
    }
}
